<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import { getCategoryListService, addCategoryService, updateCategoryService, deleteCategoryService } from '@/apis/category'


const categories = ref([])
const total = ref(0)
const params = ref({
    page: 1,
    pageSize: 5
})

const getCategoryList = async () => {
    const res = await getCategoryListService(params.value)
    categories.value = res.data.items
    total.value = res.data.total
}
onMounted(() => getCategoryList())

// 页数切换
const pageChange = (page) => {
    params.value.page = page
    getCategoryList()
}

//添加分类数据模型
const category = ref({
    name: '',
    categoryPic: ''
})

//控制添加分类弹窗
const dialogVisible = ref(false)
//控制标题展示
const title = ref('')

// 上传成功的回调函数
const uploadSuccess = (result) => {
    category.value.categoryPic = result.data
    console.log(result.data);
}

const addCategory = async () => {
    await addCategoryService(category.value)
    ElMessage.success("添加成功")
    getCategoryList()
    dialogVisible.value = false
}

//展示编辑弹窗
const showDialog = (row) => {
    dialogVisible.value = true
    title.value = '编辑分类'
    //数据拷贝
    category.value.name = row.name
    category.value.categoryPic = row.categoryPic
    category.value.id = row.id
}

const updateCategory= async () => {
    await updateCategoryService(category.value)
    ElMessage.success("修改成功")
    getCategoryList()
    dialogVisible.value = false
}
const deleteCategory = (row) => {
    ElMessageBox.confirm(
        '你确认删除该分类信息吗？',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //用户点击了确认
            await deleteCategoryService(row.id)
            ElMessage({
                type: 'success',
                message: '删除成功',
            })
            getCategoryList()
        })
        .catch(() => {
            //用户点击了取消
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}

const clearData = () => {
    category.value.name = ""
    category.value.categoryPic = ""
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>主分类</span>
                <div class="extra">
                    <el-button type="primary"
                        @click="dialogVisible = true; title = '添加分类'; clearData()">添加分类</el-button>
                </div>
            </div>
        </template>
        <el-table :data="categories" style="width: 100%">
            <el-table-column label="id" prop="id"> </el-table-column>
            <el-table-column label="分类名称" prop="name"></el-table-column>
            <el-table-column label="分类图片" prop="categoryPic">
                <template v-slot="scope">
                    <img :src="scope.row.categoryPic" :alt="'图片' + scope.$index" style="width: 100px; height: 100px;">
                </template>
            </el-table-column>
            <el-table-column label="创建时间" prop="createTime"></el-table-column>
            <el-table-column label="更新时间" prop="updateTime"></el-table-column>


            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteCategory(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页 -->
        <div class="pagination-container">
            <el-pagination :total="total" @current-change="pageChange" :page-size="params.pageSize" background
                layout="prev, pager, next" />
        </div>

        <!-- 添加分类弹窗 -->
        <el-dialog v-model="dialogVisible" :title="title" width="30%">
            <el-form :model="category" label-width="100px" style="padding-right: 30px">
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="category.name" minlength="1" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item label="分类图片">
                    <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false" action="/api/upload"
                        name="file" :on-success="uploadSuccess">
                        <img v-if="category.categoryPic" :src="category.categoryPic" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">+</el-icon>
                    </el-upload>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="title == '添加分类' ? addCategory() : updateCategory()"> 确认
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}

.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}
</style>